#include "Player.h"

Player::Player(b2World* world, int ent_type, sf::Vector2f pos, char* file, float scale, float rotation){

    type = ent_type;
    position = pos;
    angle = rotation;
    image = true;
    eScale = scale;

    texture.loadFromFile(file);
    textureRect = sf::IntRect(0,0,texture.getSize().x, texture.getSize().y);
    Init(world);
    body->SetFixedRotation(true);
    rect.setTextureRect(textureRect);
}

void Player::Update(){
    //update the animation
    if(animated){
        //std::cout << "Animated" << std::endl;
        eAnimation.Update(eclock);
        int frame = eAnimation.getCurrentFrame();
        std::cout << "Frame: " << frame << std::endl;
        textureRect.left = (frame * textureRect.width);
        rect.setTextureRect(textureRect);
        circ.setTextureRect(textureRect);
    }

    //animate if the velocity is above zero, stop if not
    if(body->GetLinearVelocity().x * SCALE != 0){
        //std::cout << "We should be moving" << std::endl;
        setFrameRate(200);
    }else{
        //std::cout << "Stop moving" << std::endl;
        setFrameRate(200); //temp test of player always animating
        //setCurrentFrame(0);
    }

    rect.setPosition(body->GetPosition().x * SCALE, body->GetPosition().y * SCALE);
    rect.setRotation(180/b2_pi * body->GetAngle());

}

void Player::setDirection(bool left){
    if (left){
        rect.setScale(sf::Vector2f(-1,1));
    } else rect.setScale(sf::Vector2f(1,1));
}
